﻿//
// File:    ConController.cs
// Project: MP3 Art Embedder v0.2.x
//

using System;
using Mp3LibraryViewed;
using Mp3Library;

namespace ConController
{
    internal interface IAppViewFactory
    {
        void Make (AppController controller, Mp3LibraryModelBase modelBase);
    }


    internal class AppController
    {
        private string[] args;
        private IAppViewFactory viewFactory;


        public AppController (string[] args, IAppViewFactory viewFactory)
        {
            this.args = args;
            this.viewFactory = viewFactory;
        }


        public int Run ()
        {
            int result = 0;

            if (args.Length > 0)
            {
                var model = new Mp3LibraryModel();

                viewFactory.Make (this, model);

                UpdateMode mode = UpdateMode.Default;
                int maxPicSize = 200;
                int argNum = 1;

                if (argNum < args.Length)
                {
                    int tryInt;
                    if (int.TryParse (args[argNum], out tryInt))
                    {
                        maxPicSize = tryInt;
                        ++argNum;
                    }
                }

                if (argNum < args.Length && args[argNum].ToUpper () == "-REPLACE")
                {
                    mode |= UpdateMode.ReplaceExisting;
                    ++argNum;
                }

                if (argNum < args.Length && args[argNum].ToUpper () == "-WHATIF")
                {
                    mode |= UpdateMode.WhatIf;
                    ++argNum;
                }

                if (argNum >= args.Length)
                {
                    try
                    {
                        model.UpdateCovers (mode, args[0], maxPicSize);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine ("Stopping on error: " + ex.Message);
                        result = 1;
                    }

                    Console.WriteLine (model.GetTotals());
                    return result;
                }
            }

            Console.WriteLine ("Usage: ConArtEmbed [<maxSize>] [-Replace] [-WhatIf]");
            return 1;
        }
    }
}
